package com.esm.sm800.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.esm.sm800.R;
import com.esm.sm800.database.dbmodel.Draw_report;
import com.esm.sm800.database.dbmodel.Hiskeyrecord;
import com.esm.sm800.database.dbmodel.Needle;
import com.esm.sm800.database.manager.DrawreportCenter;
import com.esm.sm800.database.manager.HiskeyrecordCenter;
import com.esm.sm800.database.manager.SystemCenter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.BottomMenuWindow;

//还针-断针上报信息
public class DzinfoActivity extends BaseActivity implements View.OnClickListener {

    private final String TAG = "DzinfoActivity";

    EditText et_jtname;
    EditText et_xstyle;

    EditText et_xgongxu;
    EditText et_xcolor;
    EditText et_xsize;
    EditText et_xpihao;
    EditText et_dzsm;
    EditText et_clff;

    EditText et_needle_status;

    ImageView iv_pic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_dzinfo);

        iv_pic = (ImageView) findViewById(R.id.iv_pic);
        et_jtname = (EditText) findViewById(R.id.et_jtname);
        et_xstyle = (EditText) findViewById(R.id.et_xstyle);
        et_xgongxu = (EditText) findViewById(R.id.et_xgongxu);
        et_xcolor = (EditText) findViewById(R.id.et_xcolor);
        et_xsize = (EditText) findViewById(R.id.et_xsize);
        et_xpihao = (EditText) findViewById(R.id.et_xpihao);
        et_needle_status = (EditText) findViewById(R.id.et_needle_status);
        et_dzsm = (EditText) findViewById(R.id.et_dzsm);
        et_clff = (EditText) findViewById(R.id.et_clff);
        TextView tv_cw = (TextView) findViewById(R.id.tv_cw);
        tv_cw.setText(getString(R.string.weizhi_title) + SystemCenter.getInstance().needleHz.getPosition());


        _etlist.add(et_jtname);
        _etlist.add(et_xstyle);
        _etlist.add(et_xgongxu);
        _etlist.add(et_xcolor);
        _etlist.add(et_xsize);
        _etlist.add(et_xpihao);
        _etlist.add(et_needle_status);
        _etlist.add(et_dzsm);
        _etlist.add(et_clff);

        findViewById(R.id.tv_back).setOnClickListener(this);
        findViewById(R.id.tv_save).setOnClickListener(this);

        findViewById(R.id.bt_hisrecord_jtname).setOnClickListener(this);
        findViewById(R.id.bt_hisrecord_xstyle).setOnClickListener(this);
        findViewById(R.id.bt_hisrecord_xgongxu).setOnClickListener(this);
        findViewById(R.id.bt_hisrecord_xcolor).setOnClickListener(this);
        findViewById(R.id.bt_hisrecord_xsize).setOnClickListener(this);
        findViewById(R.id.bt_hisrecord_xpihao).setOnClickListener(this);
        findViewById(R.id.bt_hisrecord_dzsm).setOnClickListener(this);
        findViewById(R.id.bt_hisrecord_clff).setOnClickListener(this);
        findViewById(R.id.bt_hisrecord_needle_status).setOnClickListener(this);

        _drEdit = SystemCenter.getInstance().drEdit;
        if (_drEdit == null) {
            finish();
            return;
        }

        if (SystemCenter.getInstance().memberTgr != null) {
            _drEdit.setTgr(SystemCenter.getInstance().memberTgr.getName());
            _drEdit.setPass_uid(SystemCenter.getInstance().memberTgr.getId());
        }

        initHisrecode();

        intPic();
    }

    private void intPic() {
        if (SystemCenter.getInstance().hzBitmap == null)
            return;
        iv_pic.setImageBitmap(SystemCenter.getInstance().hzBitmap);
    }

    ArrayList<EditText> _etlist = new ArrayList<EditText>();
    ArrayList<String[]> _arrayList = new ArrayList<>();

    Draw_report _drEdit = null;

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public static final int REFRESH_UI = 1101;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == REFRESH_UI) {

            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.bt_hisrecord_jtname:
                onHisrecord(0);
                break;
            case R.id.bt_hisrecord_xstyle:
                onHisrecord(1);
                break;
            case R.id.bt_hisrecord_xgongxu:
                onHisrecord(2);
                break;
            case R.id.bt_hisrecord_xcolor:
                onHisrecord(3);
                break;
            case R.id.bt_hisrecord_xsize:
                onHisrecord(4);
                break;
            case R.id.bt_hisrecord_xpihao:
                onHisrecord(5);
                break;
            case R.id.bt_hisrecord_needle_status:
                onHisrecord(6);
                break;
            case R.id.bt_hisrecord_dzsm:
                onHisrecord(7);
                break;
            case R.id.bt_hisrecord_clff:
                onHisrecord(8);
                break;
            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_save:
                onSave();
                break;
        }
    }

    private void onHisrecord(int pos) {
        if (_arrayList.get(pos).length < 1) {
            showShortToast(getString(R.string.wlsjl));
            return;
        }
        toActivity(BottomMenuWindow.createIntent(getBaseContext(), _arrayList.get(pos))
                .putExtra(BottomMenuWindow.INTENT_TITLE, getString(R.string.lsjl)), REQUEST_TO_BOTTOM_MENU_1 + pos, false);
    }

    //    private int type = 0; //0-登录名 1-型体 2-工序 3-颜色 4-尺码 5-批号 6-机针状态 7-断针说明 8-处理方法 9-机台编号
    private final int[] HISRECORD_TYPE = {9, 1, 2, 3, 4, 5, 6, 7, 8};

    private boolean initHisrecode() {
        _arrayList.clear();

        for (int i = 0; i < HISRECORD_TYPE.length; i++) {
            ArrayList<Hiskeyrecord> list = HiskeyrecordCenter.getInstance().getHiskeyrecordlist(HISRECORD_TYPE[i]);
            if (list == null) {
                return false;
            }
            int size = list.size();
            String[] HIS_RECORDS = new String[list.size()];
            for (int j = 0; j < size; j++) {
                HIS_RECORDS[j] = list.get(j).getName();
            }
            _arrayList.add(HIS_RECORDS);
        }

        return true;
    }

    private void onSave() {
        if (_drEdit == null) {
            return;
        }

        _drEdit.setJtname(et_jtname.getText().toString());
        _drEdit.setXstyle(et_xstyle.getText().toString());
        _drEdit.setXgongxu(et_xgongxu.getText().toString());
        _drEdit.setXcolor(et_xcolor.getText().toString());
        _drEdit.setXsize(et_xsize.getText().toString());
        _drEdit.setXpihao(et_xpihao.getText().toString());
        _drEdit.setNeedle_status(et_needle_status.getText().toString());
        _drEdit.setDzsm(et_dzsm.getText().toString());
        _drEdit.setClff(et_clff.getText().toString());
        _drEdit.setImg(SystemCenter.getInstance().hzJpgpath);

        DrawreportCenter.getInstance().saveDraw_report(_drEdit);

        //保存历史关键字
        for (int i = 0; i < _etlist.size(); i++) {
            EditText et = _etlist.get(i);
            String name = et.getText().toString();
            if (name.length() > 1) {
                Hiskeyrecord hiskeyrecord = new Hiskeyrecord();
                hiskeyrecord.setName(name);
                hiskeyrecord.setType(HISRECORD_TYPE[i]);
                //获取当前日期
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date curDate = new Date();
                String strDate = formatter.format(curDate);
                //
                hiskeyrecord.setUpdate_time(strDate);
                HiskeyrecordCenter.getInstance().saveHiskeyrecord(hiskeyrecord);
            }
        }

        setResult(RESULT_OK);
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private final int REQUEST_TO_BOTTOM_MENU_1 = 100;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }

        int index = requestCode - REQUEST_TO_BOTTOM_MENU_1;
        if (data != null && index >= 0 && index < _arrayList.size() && index < _etlist.size()) {
            int pos = data.getIntExtra(BottomMenuWindow.RESULT_ITEM_ID, -1);
            String[] HIS_RECORDS = _arrayList.get(index);
            if (pos >= 0 && pos < HIS_RECORDS.length) {
                _etlist.get(index).setText(HIS_RECORDS[pos]);
            }
        }
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }
}
